package com.xm.zcf.action.action.category;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.xm.zcf.domain.category.Category;
import com.xm.zcf.service.category.CategoryException;
import com.xm.zcf.service.category.CategoryService;
import com.xm.zcf.service.categorySecond.CategorySecondService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/3/13.
 */
@Controller
@Scope("prototype")
public class categoryAction extends ActionSupport implements ModelDriven<Category> {
    private Category model=new Category();
    private Integer cid;
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    @Resource
    private CategoryService categoryService;
    @Resource
    private CategorySecondService categorySecondService;

    public String findAll(){
        List<Category> categorylist=categoryService.findAll();
        ActionContext.getContext().put("cList",categorylist);
        return "list";
    }
    public String  addUI(){
        return "addUI";
    }
    public String add(){
        categoryService.save(model);
        return "tolist";
    }
    public String editUI(){
        Category category=categoryService.findById(model.getCid());
        ActionContext.getContext().getValueStack().push(category);
        return "editUI";
    }
    public String edit(){
        Category category=categoryService.findById(model.getCid());
        category.setCname(model.getCname());
        categoryService.update(category);
        return "tolist";
    }
    public String delete(){
        Category category=categoryService.findById(model.getCid());
        try{
            categoryService.deleteCategory(category);
        }catch (CategoryException e){
            e.printStackTrace();
            message=e.getMessage();
            return "deleteError";
        }
        return "tolist";
    }
    @Override
    public Category getModel() {
        return model;
    }
}
